﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using org.webguvenligi.NSecureImageLib.GraphicFileFormats.Utilities;
namespace org.webguvenligi.NSecureImageLib.GraphicFileFormats.PNG
{
    /// <summary>
    /// 
    /// </summary>
    public class TrnsChunk : PNGAncillaryChunkBase
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="TrnsChunk"/> class.
        /// </summary>
        /// <param name="pngReader">The PNG reader.</param>
        public TrnsChunk(BinaryReader pngReader)
            : base(pngReader)
        {
        }
        /// <summary>
        /// Parses the chunk data.
        /// </summary>
        /// <param name="chunkDataReader">The chunk data reader.</param>
        protected override void ParseChunkData(BinaryReader chunkDataReader)
        {
            base.ParseChunkData(chunkDataReader);
            IHDRChunk tempChunk =
                (IHDRChunk)(from targetChunk in this.PrecidingChunks
                            where targetChunk.GetType() == typeof(IHDRChunk)
                            select targetChunk).SingleOrDefault();
            int remainingBytes = 0;
            int depth = tempChunk.BitDepth;
            switch (tempChunk.ColorType)
            {
                case IHDRChunk.ColorEnumType.GrayScale:
                    remainingBytes = 2;
                    chunkDataReader.CheckTotalLength(remainingBytes);
                    break;
                case IHDRChunk.ColorEnumType.RGBTrible:
                    remainingBytes = 6;
                    chunkDataReader.CheckTotalLength(remainingBytes);
                    break;
                case IHDRChunk.ColorEnumType.PaletteIndex:
                    PLTEChunk currentChunk = (PLTEChunk)(from targetChunk in this.PrecidingChunks
                                                         where targetChunk.GetType() == typeof(PLTEChunk)
                                                         select targetChunk).SingleOrDefault();
                    if (currentChunk == null)
                        throw new InvalidDataException("Missing PLTE Chunk Detected");
                    remainingBytes = currentChunk.PaletteSize;
                    chunkDataReader.CheckTotalLengthLessOrEqual(remainingBytes);
                    break;
                default:
                    throw new InvalidDataException();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        private List<Type> _precidingTypes = new List<Type>()
        {
            typeof(IHDRChunk),
            typeof(PLTEChunk)
        };
        /// <summary>
        /// Gets the type of the preciding.
        /// </summary>
        /// <value>The type of the preciding.</value>
        protected override Type PrecidingType
        {
            get { return typeof(IHDRChunk); }
        }
        /// <summary>
        /// Gets the preciding types.
        /// </summary>
        /// <value>The preciding types.</value>
        protected override List<Type> PrecidingTypes
        {
            get { return _precidingTypes; }
        }
    }
}